#include "PaintArea.h"
#include "ToolFactory.h"
#include <QLabel >


Tool *aTool;
PaintArea::~PaintArea()
{
	for(int i = 0; i < undoVector.size();i++)
		delete undoVector[i];

}
 PaintArea::PaintArea(QWidget *parent) : QWidget(parent),
     theImage(500, 400,QImage::Format_ARGB32),
    // brushInterface(0),
     lastPoint(-1, -1)
 {
	 scribbling = false;
     setAttribute(Qt::WA_StaticContents);
     setAttribute(Qt::WA_NoBackground);
	 undoPointer = -1;
     theImage.fill(qRgb(255, 255, 255));
	 currentZoomLevel = 0;
 }

 void PaintArea::cleanUndoStack()
{

	for(int i = undoVector.size()-1;i > undoPointer;--i)
	{
		 delete undoVector[i];
		 undoVector.pop_back();
	}

}


 void PaintArea::resizeImage(QSize newSize)
 {
	 if (theImage.size() == newSize)
         return;

     QImage newImage(newSize, QImage::Format_RGB32);
     newImage.fill(qRgb(255, 255, 255));
     QPainter painter(&newImage);
     painter.drawImage(QPoint(0, 0), theImage);
     theImage = newImage;



 }

void PaintArea::redo()
{
	int i = undoVector.size()-1;
	if(undoPointer < i)
	{
		++undoPointer;
		theImage = *undoVector[undoPointer];
		this->resize(theImage.width(), theImage.height());
	}

}

const QString PaintArea::getCurrentTool()
{
	 const QWidget* mainWindow = this->parentWidget()->parentWidget()->parentWidget()->parentWidget()->parentWidget();
	 QString currentTool =  mainWindow->findChild<QLabel*>()->text();
	 return currentTool;
}

void PaintArea::saveUndoState()
{
	undoVector.push_back(new QImage(theImage));
	undoPointer++;
}


void PaintArea::undo()
{
	if(undoPointer >= 0)
	{
		undoPointer--;
		theImage = undoPointer == -1 ? *undoVector[undoPointer+1] : *undoVector[undoPointer];
		this->resize(theImage.width(), theImage.height());
		
	}


}

void PaintArea::mouseMoveEvent(QMouseEvent* event)
{ 
	if ((event->buttons() & Qt::LeftButton) && scribbling)
		aTool->mouseMoveEvent(event, this);
	//drawLineTo(event->pos());
}

 QSize PaintArea::sizeHint() const
 {
     return theImage.size();
 }

  void PaintArea::paintEvent(QPaintEvent *  event )
 {
     QPainter painter(this);
     painter.drawImage(QPoint(0, 0), theImage);
 }

void  PaintArea::mouseReleaseEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton && scribbling) {
          aTool->mouseReleaseEvent(event, this);

		  saveUndoState();
     }

 }



void  PaintArea::mousePressEvent(QMouseEvent *event)
 {
	 if(undoVector.size()-1 > undoPointer)
		 cleanUndoStack();
		 

     if (event->button() == Qt::LeftButton && getCurrentTool() != "") 
	 {
		 aTool = ToolFactory::createTool(getCurrentTool());
		 aTool->mousePressEvent(event, this);

	 }
	 

 }

 void PaintArea::setImage(const QImage &image)
 {
     theImage = image;
     update();
     updateGeometry();
 }


  bool PaintArea::saveImage(const QString &fileName, const char *fileFormat)
 {
	 setImage(theImage);


     if (theImage.save(fileName, fileFormat)) {
         modified = false;
         return true;
     } else {
         return false;
     }
 }

       
  bool PaintArea::openImage(const QString& fileLocation)
  {
     QImage loadedImage;
     if (!loadedImage.load(fileLocation))
         return false;

     QSize newSize = loadedImage.size().expandedTo(size());



	 setImage(loadedImage);
	 setObjectName(fileLocation);

     return true;
  }